package com.ncucoder.jwva.swing;

import com.ncucoder.jwva.service.AdminService;
import com.ncucoder.jwva.service.impl.AdminServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author <a href="https://www.ncucoder.com">hsowan</a>
 * @since 2019-01-13 03:10
 */
public class AdminSwing {
    private AdminService adminService = new AdminServiceImpl();
    private JPanel panel;

    public void login(JPanel panel) {
        this.panel = panel;
        panel.setLayout(null);

        // 设置文字居中
        JLabel titleLabel = new JLabel("欢迎登陆网站用户访问信息分析系统", JLabel.CENTER);
        titleLabel.setFont(new Font("宋体",Font.BOLD,30));
        titleLabel.setBounds(150, 60, 500, 30);
        panel.add(titleLabel);

        // 用户名
        JLabel userLabel = new JLabel("用户名:");
        userLabel.setFont(new Font("宋体", Font.PLAIN,18));
        userLabel.setBounds(275,200,70,25);
        panel.add(userLabel);

        JTextField usernameText = new JTextField(20);
        usernameText.setBounds(355,200,160,25);
        panel.add(usernameText);

        // 密码
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setFont(new Font("宋体", Font.PLAIN,18));
        passwordLabel.setBounds(275,240,70,25);
        panel.add(passwordLabel);

        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(355,240,160,25);
        panel.add(passwordText);

        // 创建登录按钮
        JButton loginButton = new JButton("登录");
        loginButton.setFont(new Font("宋体", Font.PLAIN,18));
        loginButton.setBounds(375, 290, 50, 20);
        panel.add(loginButton);

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (adminService.login(usernameText.getText(), String.valueOf(passwordText.getPassword()))){
                    panel.removeAll();
                    panel.revalidate();
                    panel.repaint();

                }else {
                    System.out.println("login fail");
                }
            }
        });
    }

    public void menu(){

    }
}
